from django.db import models
from user.models import User, UserAccountBind
from platefrom.models import Platefrom
from system.models import CurrencyLabel
from datetime import datetime

# Create your models here.

class Label(models.Model):
    """融媒体内容标签"""
    label = models.CharField(max_length=100, verbose_name='内容标签')

    class Meta:
        verbose_name = '融媒体内容标签管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.label


class ContentType(models.Model):
    """可分发媒体类别"""
    type = models.CharField(max_length=20, verbose_name='媒体类别')

    class Meta:
        verbose_name = '媒体类别管理'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.type


class Image(models.Model):
    """素材库（图片）"""
    image_label = models.ForeignKey(CurrencyLabel, blank=True, null=True, related_name='image_labels',
                                    verbose_name='图片分类', on_delete=models.SET_NULL)
    title = models.CharField(max_length=100, blank=True, null=True, verbose_name='图片标题')
    image = models.FileField(verbose_name='图片', upload_to='image/user')
    update_time = models.DateTimeField(verbose_name='上传时间', default=datetime.now)
    is_image = models.BooleanField(verbose_name="是否图片", default=True)
    creator_user = models.ForeignKey(User, verbose_name='创建者', blank=True, null=True,
                                     related_name='user_creator_image', on_delete=models.SET_NULL)

    class Meta:
        verbose_name_plural = "图库"
        app_label = 'medias'  # 添加这一行，指定所属的应用程序标签


    def delete(self, using=None, keep_parents=False):
        self.image.storage.delete(self.image.name)
        super().delete()

    def save(self, *args, **kwargs):
        if not self.title:
            self.title = self.update_time.date()
        super().save(*args, **kwargs)



class MediaContent(models.Model):
    """用户融媒体内容分发"""

    label = models.CharField(max_length=200, verbose_name='主题', blank=True, null=True)

    title = models.CharField(max_length=200, verbose_name='主题', blank=True, null=True)
    content = models.TextField(verbose_name='文章内容', blank=True, null=True)
    # 合并
    media = models.TextField(verbose_name='媒体内容', blank=True, null=True)
    poi_name = models.CharField(max_length=100, verbose_name='地理位置名称', blank=True, null=True)
    cover_image_id = models.CharField(max_length=100, verbose_name='封面图片ID', blank=True, null=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True, blank=True, null=True)
    TYPES = (('saves', '保存'), ('checking', '审核中'), ('pass', '已通过'), ('no_pass', '未通过'),
             ('distribute', '分发中'), ('pass_distribute', '已分发'), ('error_distribute', '分发错误'),
             ('delete', '回收站'))
    types = models.CharField(choices=TYPES, verbose_name='审核状态', max_length=20)
    desc = models.CharField(verbose_name='媒体描述', max_length=256, blank=True, null=True)
    content_type = models.ForeignKey(ContentType, null=True, blank=True, on_delete=models.SET_NULL,
                                     verbose_name='媒体类型')  # 添加媒体类型
    disagree_reason = models.CharField(verbose_name='拒绝理由', default='未通过', max_length=200, blank=True, null=True)
    is_delete = models.BooleanField(verbose_name="删除状态", default=False)
    order = models.IntegerField(verbose_name='媒体排序', default=50)
    creator_user = models.ForeignKey(User, verbose_name='创建者', blank=True, null=True,
                                     related_name='user_creator_MediaContent', on_delete=models.SET_NULL)
    skus_id = models.CharField(max_length=200, verbose_name='冗余skus_id', blank=True, null=True)
    platefroms_id = models.CharField(max_length=200, verbose_name='冗余platefroms_id', blank=True, null=True)
    accounts_mobiles = models.CharField(max_length=255, verbose_name='冗余accounts_mobiles', blank=True, null=True)
    reviewed_user = models.ForeignKey(User, verbose_name='审核人', blank=True, null=True,
                                      related_name='user_reviewed_users', on_delete=models.SET_NULL)

    def __str__(self):
        return str(
            self.add_time) + self.get_types_display().center(8, "-") + self.title

    class Meta:
        verbose_name = '融媒体内容管理'
        verbose_name_plural = verbose_name


class AccountBindMediaContent(models.Model):
    """媒体账号绑定"""
    account_id = models.CharField(max_length=200, verbose_name='账户id', null=True, blank=True)
    account = models.CharField(max_length=200, verbose_name='账户-手机号', null=True, blank=True)
    android_login_file = models.CharField(max_length=200, verbose_name='账户-登录文件地址', null=True, blank=True)
    media_content = models.ForeignKey(MediaContent, verbose_name='融媒体内容', on_delete=models.CASCADE, blank=True,
                                      null=True, related_name='account_midas_content')
    TYPES = ((0,'准备分发'),(1, '审核中'), (2, '未通过'), (3, '分发中'), (4, '分发成功'),(5, '分发错误'))
    status = models.IntegerField(choices=TYPES, verbose_name='发布状态', default=1)
    msg = models.CharField(max_length=200, verbose_name='提示信息', null=True, blank=True)
    media_content_in_platform_id = models.CharField(max_length=200, verbose_name='作品的平台ID', null=True, blank=True)
    response_info = models.TextField(verbose_name='作品发布后的回调结果', null=True, blank=True)
    media_title = models.CharField(max_length=200, verbose_name='标题', null=True)
    media_desc = models.CharField(max_length=200, verbose_name='简介', null=True)
    platform_name = models.CharField(max_length=200, verbose_name='平台名字', null=True)
    priority = models.IntegerField(default=1, verbose_name='优先级', null=True)
    url = models.CharField(max_length=200, verbose_name='内容url', null=True)
    add_time = models.DateTimeField(verbose_name='添加时间',auto_now_add=True, blank=True, null=True)
    views_count = models.CharField(max_length=200, verbose_name='浏览量', null=True)
    likes_count = models.CharField(max_length=200, verbose_name='点赞量', null=True)
    collect_count = models.CharField(max_length=200, verbose_name='收藏量', null=True)

    class Meta:
        verbose_name = '融媒体账号内容绑定管理'
        verbose_name_plural = verbose_name

